package com.cbs.android.module.userkit.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.cbs.android.component.application.activity.CBSActivity;
import com.cbs.android.component.application.activity.handler.CBSActivityResultHandler;
import com.cbs.android.component.application.activity.handler.CBSSimpleActivityResultHandler;
import com.cbs.android.component.applicationutils.network.ErrorResponse;
import com.cbs.android.component.log.L;
import com.cbs.android.component.toast.Toast;
import com.cbs.android.module.user.user.UserModule;
import com.cbs.android.module.user.user.entity.User;
import com.cbs.android.module.user.user.handler.ModuleHandler;
import com.cbs.android.module.userkit.R;
import com.cbs.android.module.userkit.UserKit;
import com.cbs.android.widget.loadingframelayout.LoadingFrameLayout;
import com.google.gson.Gson;


/**
 * Created by broche on 10/30/15.
 */
public class LoginActivity extends CBSActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private static final String TAG = LoginActivity.class.getName();
    private EditText userNameView;
    private EditText passwordView;
    private CheckBox showPasswordView;
    private LoadingFrameLayout loadingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);

        super.setContentView(R.layout.cbs_userkit_activity_login);
        userNameView = (EditText) findViewById(R.id.cbs_userkit_login_username);
        passwordView = (EditText) findViewById(R.id.cbs_userkit_login_password);
        showPasswordView = (CheckBox) findViewById(R.id.cbs_userkit_login_showpassword);
        loadingView = (LoadingFrameLayout) findViewById(R.id.cbs_userkit_login_loading);


        findViewById(R.id.cbs_userkit_login_back).setOnClickListener(this);
        findViewById(R.id.cbs_userkit_login_forgotpassword).setOnClickListener(this);
        findViewById(R.id.cbs_userkit_login_register).setOnClickListener(this);
        findViewById(R.id.cbs_userkit_login_submit).setOnClickListener(this);
        showPasswordView.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        int action = v.getId();
        if (action == R.id.cbs_userkit_login_back) {
            finish();
        } else if (action == R.id.cbs_userkit_login_submit) {
            loadingView.startLoading();
            UserModule.login(userNameView.getText().toString().trim(), passwordView.getText().toString(), new ModuleHandler<User>() {
                @Override
                public void onSuccess(User entity) {
                    loadingView.stopLoading();
                    setResult(RESULT_OK);
                    finish();
                }

                @Override
                public void onFailure(int code, String message) {
                    loadingView.stopLoading();
                    ErrorResponse errorResponse = new Gson().fromJson(message, ErrorResponse.class);
                    Toast.show(errorResponse.getErrorMsg());
                    L.e(TAG, "code: " + code + "\tmessage: " + message);
                }

                @Override
                public void onException(Exception exception) {
                    loadingView.stopLoading();
                    Toast.show(getString(R.string.cbs_userkit_hint_login_unknow));
                    L.e(TAG, "", exception);
                }
            });
        } else if (action == R.id.cbs_userkit_login_forgotpassword) {
            UserKit.forgotPassword(LoginActivity.this, new CBSActivityResultHandler() {
                @Override
                public void onActivityResult(int requestCode, int resultCode, Intent data) {
                    // do nothing
                }
            });
        } else if (action == R.id.cbs_userkit_login_register) {
            UserKit.register(LoginActivity.this, new CBSSimpleActivityResultHandler() {
                @Override
                public void onSuccess(int requestCode, Intent data) {
                    setResult(RESULT_OK);
                    finish();
                }

                @Override
                public void onCancel(int requestCode, Intent data) {
                    // do nothing
                }
            });
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            passwordView.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            passwordView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
        Editable etable = passwordView.getText();
        Selection.setSelection(etable, etable.length());
    }
}
